package com.example.document

import com.example.document.entity.DocEntity
import com.example.lib_base.BaseEntity
import com.example.lib_base.BaseRepository
import com.example.lib_http.HttpType
import com.example.lib_http.NetWorkFactory
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import java.io.File

class DocRepository:BaseRepository() {

    fun selDocuments(map:Map<String,Any>):Flow<DocEntity>{
        val documents = NetWorkFactory.factory(HttpType.NONE)
            .create(ApiService::class.java)
            .selDocuments(createBody(map))

        return documents
    }

    fun delDocuments(map:Map<String,Any>):Flow<DocEntity>{
        val flow = NetWorkFactory.factory(HttpType.NONE)
            .create(ApiService::class.java)
            .delDocuments(createBody(map))
        return flow
    }

    fun uploadDoc(map: Map<String, Any>):Flow<BaseEntity>{
        val path = map.get("path") as String
        val name = map.get("name") as String
        val body=RequestBody.create(MediaType.parse("multipart/form-data"), File(path))

        val builder = MultipartBody.Builder()
        builder.addFormDataPart("file",name,body)

    /*    val request =
            Request.Builder().url("http://10.161.9.80:8383/document/uploaddocuments").addHeader("Content-Type", "multipart/form-data").
        val response = OkHttpClient.Builder().build().newCall(request).execute()*/

        val flow = NetWorkFactory.factory(HttpType.NONE)
            .create(ApiService::class.java)
            .uploadDoc(builder.build().parts())
        return flow
    }
}